/*
 * Copyright 2008-2009 Daniel Freitas
 * 
 * This file is part of Army Creator.
 * 
 * Army Creator is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Army Creator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with Army Creator. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package com.googlecode.armycreator.domain;

import java.util.UUID;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;

import org.hibernate.annotations.Immutable;

public class Roster implements IRoster {

    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private Army army;
    private Integer pointsLimit;
    @Immutable
    private final UUID uuid;
    @Transient
    private Exporter beanExporter;

    private Roster(Builder builder) {
        this.name = builder.name;
        this.army = builder.army;
        this.pointsLimit = builder.pointsLimit;
        this.uuid = builder.uuid;
        this.beanExporter = new Exporter();
    }

    public Exporter exportAsBean() {
        return beanExporter;
    }

    // Equals and hash code
    // -----------------------------------------------------------------------------------------------------------------
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((army == null) ? 0 : army.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof Roster)) {
            return false;
        }
        
        Roster other = (Roster) obj;
        
        if (army == null) {
            if (other.army != null) {
                return false;
            }
        } else if (!army.equals(other.army)) {
            return false;
        }
        if (name == null) {
            if (other.name != null) {
                return false;
            }
        } else if (!name.equals(other.name)) {
            return false;
        }
        if (uuid == null) {
            if (other.uuid != null) {
                return false;
            }
        } else if (!uuid.equals(other.uuid)) {
            return false;
        }
        return true;
    }
    
    // Builder and exporter pattern
    // -----------------------------------------------------------------------------------------------------------------
    public static class Builder {
        private String name;
        private Army army;
        private Integer pointsLimit;
        private UUID uuid;

        public Builder(String name, Army army) {
            this.name = name;
            this.army = army;
        }

        public Builder pointsLimit(Integer pointsLimit) {
            this.pointsLimit = pointsLimit;
            return this;
        }

        public Builder uuid(UUID uuid) {
            this.uuid = uuid;
            return this;
        }

        public Roster build() {
            if (uuid == null) {
                uuid = UUID.randomUUID();
            }

            return new Roster(this);
        }
    }

    public class Exporter {

        public String getName() {
            return name;
        }

        public Army getArmy() {
            return army;
        }

        public Integer getPointsLimit() {
            return pointsLimit;
        }

        public UUID getUuid() {
            return uuid;
        }

        public Long getId() {
            return id;
        }
    }

    private static final long serialVersionUID = 6736253347420570999L;
}
